﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Controller;
using DataAccess;
using Entity;

namespace Controller
{
    public class StorageController:ControllerBase
    {

        public void addStorage(int stockInId, int stockItemId, int qty, int vendPoItemId)
        {
            Storage storage=new Storage();
            storage.StockInId = stockInId;
            storage.StockItemId = stockItemId;
            storage.Qty = qty;
            storage.VendPOItemId = vendPoItemId;
            storage.Remark = "";
            Sdc.Storages.InsertOnSubmit(storage);
            Sdc.SubmitChanges();
            
        }

        public IList<StorageEntity> FindStorageByInvoiceId(int vendorInvoiceId)
        {
            VendInvoice vendInvoice = (from v in Sdc.VendInvoices
                                       where v.Id == vendorInvoiceId
                                       select v).SingleOrDefault();
            
            IList<Storage> storages = (from s in Sdc.Storages
                                       where s.StockInId == vendInvoice.StockInId
                                       select s).ToList();
            IList<StorageEntity> storageEntities=new List<StorageEntity>();
            foreach (Storage storage in storages)
            {
                StorageEntity storageEntity = TransformStorageToStorageEntity(storage);
                storageEntities.Add(storageEntity);
            }
            return storageEntities;
        }

        public static StorageEntity TransformStorageToStorageEntity(Storage storage)
        {
            StorageEntity storageEntity=new StorageEntity();
            storageEntity.ProductId = storage.VendPOItem.Product.Id;
            storageEntity.ProductNo = storage.VendPOItem.Product.No;
            storageEntity.ProductName = storage.VendPOItem.Product.Name;
            storageEntity.BatchNo = storage.StockItem.BatchNo;
            storageEntity.Quantity = storage.Qty;
            storageEntity.PoNo = storage.VendPOItem.VendPO.No;
            storageEntity.PoId = storage.VendPOItem.VendPOId;
            return storageEntity;
        }
    }
}
